package psrc.logic.pages.options;

import psrc.logic.pages.base.ControllerBase;
import psrc.logic.util.HashSHA256;

public class PasswordChangeController extends ControllerBase {

	private static final long serialVersionUID = 1L;
	private String actualPassword;
	private String newPassword;
	private String confirmNewPassword;

	@Override
	public String executeController() throws Exception {
		boolean isError = false;
		if (!newPassword.equals(confirmNewPassword)) {
			addActionError(getText("passwordchange.unmatch"));
			isError = true;
		} else {
			String actualInBase = PasswordChangeModel.getPassword();
			if(HashSHA256.encode(actualPassword).equals(actualInBase)){
				PasswordChangeModel.setNewPassword(HashSHA256.encode(newPassword));
				addActionMessage(getText("passwordchange.success"));
			} else {
				addActionError(getText("passwordchange.invalidactual"));
				isError = true;
			}
				
		}
		
		if (isError) {
			return ERROR;
		} else {
			return SUCCESS;
		}
	}

	/**
	 * @return the actualPassword
	 */
	public String getActualPassword() {
		return actualPassword;
	}

	/**
	 * @param actualPassword
	 *            the actualPassword to set
	 */
	public void setActualPassword(String actualPassword) {
		this.actualPassword = actualPassword;
	}

	/**
	 * @return the newPassword
	 */
	public String getNewPassword() {
		return newPassword;
	}

	/**
	 * @param newPassword
	 *            the newPassword to set
	 */
	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	/**
	 * @return the confirmNewPassword
	 */
	public String getConfirmNewPassword() {
		return confirmNewPassword;
	}

	/**
	 * @param confirmNewPassword
	 *            the confirmNewPassword to set
	 */
	public void setConfirmNewPassword(String confirmNewPassword) {
		this.confirmNewPassword = confirmNewPassword;
	}

}
